function show_panel(p){
		if(p == "dctype_panel"){
			$("#dict_panel").hide();
		}else{
			$("#dctype_panel").hide();
		}
		$("#"+p).show();
	}
var curDictId = null;
$(function() {
	$("#grid-table").jqGrid({
		 url:'../dict/queryDictLike.do',
		 postData:{'name':""},
		 mtype: 'POST',
		 datatype : "json",
		multiselect: true,
		multiboxonly:true,
		colModel:[
			{name:'id', hidden:true  },
			{label:'字典名称',name:'name', width:100, editable:false  },
			{label:'字典编码', name:'code', width:100, editable:false },
			 
		 ],
		 jsonReader: {  
		 	root:"results", page:"pageNum", total:"totalPageNum",          //   很重要 定义了 后台分页参数的名字。
		 	records:"totalCount", id : "id"
		 },
		viewrecords : true,
		rowNum:15,
		pager : "#grid-pager",
		altRows: true,
		loadComplete : function() {
			updatePagerIcons(this);
			enableTooltips(this);
		},
		onPaging: function(pgButton) {      
		},
		onSelectRow: function (rowid, status) {
			showType();
		},
		autowidth: true
	});

	$("#grid-dctype").jqGrid({
		 url:'../dict/queryDictType.do',
		 postData:{'dictId':""},
		 mtype: 'POST',
		 datatype : "json",
		 colModel:[
			{name:'id', hidden:true  },
			{label:'字典类型名称',name:'name', width:"250", editable:false  },
			{label:'字典类型编码', name:'code', width:"250", editable:false },
		 ],
		 multiselect: true,
		 multiboxonly:true,
		 loadComplete : function() {
				updatePagerIcons(this);
				enableTooltips(this);
			},
		viewrecords : true,
		altRows: true,
		autowidth: true 
	}); 
	
	validata();
});

function addDict(){
	clearform('dictform');
	$("#dict_panel .title").text("字典添加");
	show_panel("dict_panel");
}

function doSaveDict(){
	$("#dictform").submit();
}

function editDict(){
	var grid = $("#grid-table");
	var ids = RM.jqgrid.ids(grid);
	if(ids.length == 0 || ids.length > 1){
		RM.msg("请选择一行数据进行编辑");
	}else{
		clearform('dictform');
		$("#dict_panel .title").text("字典编辑");
	    var rowsData = $('#grid-table').jqGrid('getRowData',ids[0]);
		$('#form_dict_id').val(rowsData.id);
	    $('#form_dict_name').val(rowsData.name);
	    $('#form_dict_code').val(rowsData.code);
	    show_panel("dict_panel");
	}
}

function deleteDict(){
	RM.jqgrid.dels($("#grid-table"),"../dict/dels.do");
	$('#dict_panel').hide();
	$('#dctype_panel').hide();
}


function showType(){
	var grid = $("#grid-table");
	var ids = RM.jqgrid.ids(grid);
	if(ids.length == 0 ){
		RM.msg("请选择一行数据进行类型管理");
		return ;
	}
	if( ids.length > 1){
		$("#dctype_panel").hide();
		return ;
	}
	else{
		$("#grid-dctype").jqGrid('setGridParam',{  
	        datatype:'json',  
	        postData:{'dictId':ids[0]},
	    }).trigger("reloadGrid"); //重新载入
		show_panel("dctype_panel");
		curDictId = ids[0];
	}
}

function editDctype(){
	var grid = $("#grid-dctype");
	var ids = RM.jqgrid.ids(grid);
	if(ids.length == 0 || ids.length > 1){
		RM.msg("请选择一行数据进行编辑");
	}else{
		clearform('dctypeform');
		RM.dialog("编辑", $("#dctypeinfo").html());
		var rowsData = $('#grid-dctype').jqGrid('getRowData',ids[0]);
		$('.layui-layer #form_dctype_dict_id').val(rowsData.id);
	    $('.layui-layer #form_dctype_dict_name').val(rowsData.name);
	    $('.layui-layer #form_dctype_dict_code').val(rowsData.code);
	    $(".layui-layer #form_dctype_parent_id").val(curDictId);
	    
	    var row = $('#grid-table').jqGrid('getRowData',curDictId);
	    $('.layui-layer #form_dict_text').val(row.name);
	    
	    validata();
	}
	
}
function addDctype(){
//		$('#dctypeform')[0].reset();
	if(curDictId == null){
		RM.alert("请先选择数据字典");
		return ;
	}
	
	clearform('dctypeform');
	RM.dialog("添加", $("#dctypeinfo").html());
	$(".layui-layer #form_dctype_parent_id").val(curDictId)
	var row = $('#grid-table').jqGrid('getRowData',curDictId);
    $('.layui-layer #form_dict_text').val(row.name);
    validata();
    
}
function deleteDctype(){
	RM.jqgrid.dels($("#grid-dctype"),"../dict/dels.do");
}

function doSaveDctype(){
	$(".layui-layer #dctypeform").submit();
}

var postFlag = false;

function clearform(formid){
	$('#'+formid)[0].reset();
    $("#"+formid+" :input").not(":button, :submit, :reset, .clear_save ").val("").removeAttr("checked").remove("selected"); 
    $("#"+formid+" .error").html("");
}

function sendMyAjax(url,data,callback){
	postFlag = true;
	$.ajax({
		   type: "POST",
		   url:url,
		   data:data, 
		   success: function(msg){
			   if(msg.code != '0'){
				  RM.alert(msg.message);
			   }else{
				 callback(); 
			   }
			   postFlag = false;
		   } ,
		   error: function(error){
			   RM.alert(error);
			   postFlag = false;
		   }
	});
}

function validata(){
	$("#dictform").validate({
		submitHandler: function() {
			if(!postFlag){
				postFlag = true;
				$.ajax({
					   type: "POST",
					   url:"../dict/save.do",
					   data:$('#dictform').serialize(),// 要提交的表单
					   success: function(msg) {
						   $("#grid-table").jqGrid().trigger("reloadGrid");  
						   $('#dict_panel').hide();
						   postFlag = false;
						},
					   error: function(error){
						   alert(error);
						   postFlag = false;
					   }
				});
			}
		}
	});
	
	$(".layui-layer #dctypeform").validate({
		submitHandler: function() {
			if($(".layui-layer #form_dctype_parent_id").val() == null){
				RM.alert("请先选择词典");
				return null;
			}
			if(!postFlag){
				postFlag = true;
				$.ajax({
					   type: "POST",
					   url:"../dict/save.do",
					   data:$('.layui-layer #dctypeform').serialize(),// 要提交的表单
					   success: function(msg) {
						   $("#grid-dctype").jqGrid().trigger("reloadGrid");  
						   RM.close();
						   postFlag = false;
						},
					   error: function(error){
						   alert(error);
						   postFlag = false;
					   }
				});
			}
		}
	});
}